/**
 * Created by IntelliJ IDEA.
 * User: mawl
 * Date: 12-10-26
 * Time: 15:21
 * Register page functions.
 */

/**
 * 第一个注册页面
 */
$(document).ready(function() {
	
	function trim(strToTrim) { 
        result=strToTrim; 
        for (var _i=0; _i<strToTrim.length; _i++) { 
                if (strToTrim.charAt(_i)==" "){ 
                	result=strToTrim.substr(_i+1);
                }else{
                	_i=strToTrim.length; 
                }
        } 
        strToTrim=result;
        for (var _i=strToTrim.length-1; _i>=0; _i--) { 
                if (strToTrim.charAt(_i)==" ") {
                	result=strToTrim.substr(0,_i);
                }else{
                	_i=0; 
                }
        } 
   		return result;
	} 
	
	$("#email_default").bind("blur", function(){
		$("#regUserForm").find("[for='email_default']").remove();
		if( ! checkEmail($(this).val())){
			var errorImg = $("<img></img>")
				.attr("for", "email_default")
				.attr("title", getLanMsgs("0ec22180-100171"))
				.attr("src","/ebvp/commonres/common/frame/src/img/validate_error.png")
				.addClass("error")
				.attr("style", "position:absolute; left:225px; top:1px");
			errorImg.insertAfter($(this));
		}
	});
	
	$("#supusername").bind("blur", function(){
		checkUsernameUnique();
	});
	
	function checkUsernameUnique(){
		$("#regUserForm").find("[for='username_unique']").remove();
		var myts = new Date();
		jQuery.ajax( {
			type : "GET",
			async : false,
			url : ebvp_context + "/register/checkusernameunique?username="
					+ $("#supusername").val() + "&myts=" + myts,
			dataType : "text",
			success : function(result) {
				if (result == 'NULL' || result=="") {
					return true;
				}else if(result.indexOf("ERROR:") != -1){
					$("[for='name_error']").html(result);
					return false;
				}else {
					var errMsg = getLanMsgs("0ec22180-100205");
					var errLabel = $("<img></img>").attr("for", "username_unique")
									.attr("src",
											"/ebvp/commonres/common/frame/src/img/validate_error.png")
									.addClass("error")
									.attr("title", errMsg).attr(
											"style",
											"position:absolute; left:225px; top:1px");
					errLabel.insertAfter($("#supusername"));
					return false;
				}
			},
			error:function(result){
				return true;
			}
		});
	}
	
	$("#regUserSubmit").bind('click', function() {
		
		var reg_val = $("#tel_reg_default").val();
		var phone_val = $("#tel_phone_default").val();
		var ext_val = $("#tel_ext_default").val();
		
		if(reg_val != null && trim(reg_val) != ""){
			$("#tel_default").val(trim(reg_val));
		}
		if(phone_val != null && trim(phone_val) != ""){
			$("#tel_default").val(trim(reg_val) + "-" + trim(phone_val));
		}
		if(ext_val != null && trim(ext_val) != ""){
			$("#tel_default").val(trim(reg_val) + "-" + trim(phone_val) + "-" + trim(ext_val));
		}
		//IE10不支持这种调用方法
		//$.commonValidator_regUserForm(); 
		if($("#rgAgree").attr("checked")!="checked"){
			$("[for='rule_error']").text(getLanMsgs("0ec22180-100186"));
			return;
		}
		
		$("#regUserForm").find("[for='username_unique']").remove();
		var myts = new Date();
		jQuery.ajax( {
			type : "GET",
			async : false,
			url : ebvp_context + "/register/checkusernameunique?username="
					+ $("#supusername").val() + "&myts=" + myts,
			dataType : "text",
			success : function(result) {
				if (result == 'NULL') {
					;
				}else if(result.indexOf("ERROR:") != -1){
					$("[for='name_error']").html(result.substring(6));
					return false;
				} else {
					var errMsg = getLanMsgs("0ec22180-100205");
					var errLabel = $("<img></img>").attr("for", "username_unique")
									.attr("src",
											"/ebvp/commonres/common/frame/src/img/validate_error.png")
									.addClass("error")
									.attr("title", errMsg).attr(
											"style",
											"position:absolute; left:225px; top:1px");
					errLabel.insertAfter($("#supusername"));
				}
			}
		});
		
		if($("#regUserForm").find("[for='username_unique']").length != 0){
			return;
		}
		
		if($("[for='name_error']").html() != null && $("[for='name_error']").html().indexOf("ERROR") != -1){
			return ;
		}
		
		$("#regUserForm").find("[for='email_default']").remove();
		if( ! checkEmail($("#email_default").val())){
			var errorImg = $("<img></img>")
				.attr("for", "email_default")
				.attr("title", getLanMsgs("0ec22180-100171"))
				.attr("src","/ebvp/commonres/common/frame/src/img/validate_error.png")
				.addClass("error")
				.attr("style", "position:absolute; left:225px; top:1px;");
			errorImg.insertAfter($("#email_default"));
			return;
		}
		
		$("#regUserForm").attr("action",ebvp_context+"/register/reginfo");
		$("#regUserForm").submit();
	});
	
});

